<?php

namespace app\report\queue;

use app\admin\queue\JobsQueue;
use service\ExcelService;
use service\ZipService;
use think\console\Output;
use think\Db;
use think\Exception;
use think\exception\HttpResponseException;

/**
 * 任务队列 - 统计数据
 * Class CountQueue
 * @package app\company\queue
 */
class CountQueue extends JobsQueue
{
    /**
     * 当前任务URI
     */
    const URI = self::class;

    /**
     * CountQueue constructor.
     */
    public function __construct()
    {
        $this->output = new Output();
    }

    /**
     * 执行业务指令
     * @return bool
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function execute()
    {
        try {
            $file = env('root_path') . 'public/' . substr($this->data['zipFile'], strpos($this->data['zipFile'], 'upload/'));
            $unzipDir = env('root_path') . 'public/' . $this->data['type'];
            if (!is_dir($unzipDir)) mkdir($unzipDir, 0755);
            ZipService::unzip($file, $unzipDir);
            Db::execute("truncate table report_{$this->data['type']}_us");
            $scanFile = glob("{$unzipDir}/*");
            foreach ($scanFile as $fileName) {
                $shopName = explode('.', basename(iconv('GBK', 'UTF-8', $fileName)))[0];
                switch ($this->data['type']) {
                    case 'ad':
                        $data = ExcelService::import($fileName, 2);
                        foreach ($data as $vo) {
                            Db::name('ReportAdUs')->insert([
                                'store' => $shopName,
                                'msku'  => $vo['G'],
                                'money' => str_replace('$ ', '', $vo['M']),
                            ]);
                        }
                        break;

                    case 'trade':
                        $data = ExcelService::import($fileName, 9);
                        foreach ($data as $vo) {
                            Db::name('ReportTradeUs')->insert([
                                'store'                       => $shopName,
                                'type'                        => $vo['C'],
                                'sku'                         => $vo['E'],
                                'description'                 => $vo['F'],
                                'quantity'                    => $vo['G'],
                                'product_sales'               => $vo['M'],
                                'shipping_credits'            => $vo['N'],
                                'gift_wrap_credits'           => $vo['O'],
                                'promotional_rebates'         => $vo['P'],
                                'sales_tax_collected'         => $vo['Q'],
                                'marketplace_facilitator_tax' => $vo['R'],
                                'selling_fees'                => $vo['S'],
                                'fba_fees'                    => $vo['T'],
                                'other_transaction_fees'      => $vo['U'],
                                'other'                       => $vo['V'],
                                'total'                       => $vo['W'],
                            ]);
                        }
                        break;

                    case 'storage':
                        $data = ExcelService::import($fileName, 2);
                        foreach ($data as $vo) {
                            Db::name('ReportStorageUs')->insert([
                                'store'                   => $shopName,
                                'msku'                    => $vo[1],
                                'fnsku'                   => $vo[2],
                                'asin'                    => $vo[3],
                                'inv_age_0_to_90_days'    => $vo[8],
                                'inv_age_91_to_180_days'  => $vo[9],
                                'inv_age_181_to_270_days' => $vo[10],
                                'inv_age_271_to_365_days' => $vo[11],
                                'inv_age_365_plus_days'   => $vo[12],
                            ]);
                        }
                        break;

                    case 'refund':
                        $data = ExcelService::import($fileName, 2);
                        foreach ($data as $vo) {
                            Db::name('ReportRefundUs')->insert([
                                'store'    => $shopName,
                                'msku'     => $vo[2],
                                'quantity' => $vo[6],
                                'status'   => $vo[10],
                            ]);
                        }
                        break;

                    case 'order':
                        $data = ExcelService::import($fileName, 2);
                        foreach ($data as $vo) {
                            Db::name('ReportOrderUs')->insert([
                                'store'               => $shopName,
                                'order_type'          => $vo[2],
                                'order_status'        => $vo[3],
                                'msku'                => $vo[5],
                                'fnsku'               => $vo[6],
                                'disposition'         => $vo[7],
                                'requested_quantity'  => $vo[8],
                                'cancelled_quantity'  => $vo[9],
                                'disposed_quantity'   => $vo[10],
                                'shipped_quantity'    => $vo[11],
                                'in_process_quantity' => $vo[12],
                            ]);
                        }
                        break;

                }
            }
        } catch (\Exception $e) {
            $this->statusDesc = $e->getMessage() . $e->getFile() . '[' . $e->getLine() . ']';
            return false;
        }

        $this->output->writeln('生成报告成功');
        $this->statusDesc = '生成报告成功';
        return true;
    }


}